package com.jtech.together.view.activity;

import android.os.Bundle;
import android.widget.Toast;

import com.jtech.together.R;
import com.jtech.together.manager.UserManager;
import com.jtech.together.mvp.contract.SplashContract;
import com.jtech.together.mvp.presenter.SplashPresenter;
import com.jtech.together.util.ActivityJump;
import com.jtech.together.view.activity.base.BaseActivity;
import com.karumi.dexter.Dexter;
import com.karumi.dexter.MultiplePermissionsReport;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.multi.MultiplePermissionsListener;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

/**
 * 起始页
 * Created by wuxubaiyang on 2017/5/10.
 */
public class SplashActivity extends BaseActivity implements SplashContract.View, MultiplePermissionsListener {
    private SplashContract.Presenter presenter;

    @Override
    protected void initVariables(Bundle bundle) {
        //绑定P类
        presenter = new SplashPresenter(getActivity(), this, bundle);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_splash);
    }

    @Override
    protected void loadData() {
        Dexter.withActivity(getActivity())
                .withPermissions(presenter.getPermissions())
                .withListener(this)
                .check();
    }

    @Override
    public void onPermissionsChecked(MultiplePermissionsReport report) {
        if (!report.areAllPermissionsGranted()) {
            Toast.makeText(getActivity(), "未能获取到权限", Toast.LENGTH_SHORT).show();
            return;
        }
        next();
    }

    @Override
    public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
        token.continuePermissionRequest();
    }

    /**
     * 接下来的动作
     */
    private void next() {
        if (UserManager.isLogin()) { //如果以登录则跳转到首页
            ActivityJump.build(getActivity(), MainActivity.class)
                    .jump();
        } else {//未登录则跳转到注册页面
            ActivityJump.build(getActivity(), RegisterActivity.class)
                    .jump();
        }
        onBackPressed();
    }
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(TAG);
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(TAG);
        MobclickAgent.onPause(this);
    }
}